package cn.kent.heima.high.garbage;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 演示 软引用
 * 引用类型分类如下:
 * 强引用
 * 软引用
 * 弱引用
 * 虚引用
 * 终结器引用
 *
 * -Xmx20m -XX:+PrintGCDetails -verbose:gc
 */
public class Demo_14 {

    private static final int _4Mb = 1024 * 1024 * 4;

    public static void main(String[] args) {
        soft();
    }

    public static void soft() {
        // list -> SoftReference -> byte[]

        List<SoftReference<byte[]>> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            SoftReference<byte[]> ref = new SoftReference<>(new byte[_4Mb]);
            System.out.println(ref.get());
            list.add(ref);
            System.out.println(list.size());
        }
        System.out.println("循环结束:" + list.size());
        for (SoftReference<byte[]> ref : list) {
            System.out.println(ref.get());
        }
    }

}
